package com.rc.saas.tenant.service.ugs;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.mapper.ugs.TenantUgsLevelGainMapper;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelGain;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelGainExample;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelGainDto;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelGainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author frog
 * @date 2021/4/26
 */
@Service
public class TenantUgsLevelGainServiceImpl implements TenantUgsLevelGainService {

    @Autowired
    private TenantUgsLevelGainMapper tenantUgsLevelGainMapper;

    @Autowired
    private UpYunConfig upYunConfig;

    @Override
    public TenantUgsLevelGainMapper getMapper() {
        return tenantUgsLevelGainMapper;
    }

    @Override
    public PageInfo<TenantUgsLevelGainVo> listPageTenantUgsLevelGain(TenantUgsLevelGainDto tenantUgsLevelGainDTO) {
        PageHelper.startPage(tenantUgsLevelGainDTO.getOffset(), tenantUgsLevelGainDTO.getLimit());
        TenantUgsLevelGainExample example = new TenantUgsLevelGainExample();
        example.createCriteria().andTenantCodeEqualTo(tenantUgsLevelGainDTO.getTenantCode());
        List<TenantUgsLevelGainVo> tenantUgsLevelGains = tenantUgsLevelGainMapper.listPageTenantUgsLevelGain(tenantUgsLevelGainDTO);
        for (TenantUgsLevelGainVo tenantUgsLevelGain : tenantUgsLevelGains) {
            tenantUgsLevelGain.setGainTypeImg(upYunConfig.getDownloadRoot() + tenantUgsLevelGain.getGainTypeImg());
        }

        return new PageInfo<TenantUgsLevelGainVo>(tenantUgsLevelGains);
    }

    @Override
    public TResponse updateStatus(Long levelGainTypeId) {
        TenantUgsLevelGain tenantUgsLevelGain = tenantUgsLevelGainMapper.selectByPrimaryKey(levelGainTypeId);
        if ("0".equals(tenantUgsLevelGain.getStatus())) {
            tenantUgsLevelGain.setStatus("1");
        } else if ("1".equals(tenantUgsLevelGain.getStatus())) {
            tenantUgsLevelGain.setStatus("0");
        }
        tenantUgsLevelGainMapper.updateByPrimaryKeySelective(tenantUgsLevelGain);
        return TResponse.SUCCESS();
    }
}
